home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / ppl4c10.zip / SI.C < prev    next >
Text File  |  1995-02-13  |  12KB  |  505 lines

  1. /*
  2. **
  3. **   --- please read this ! ---
  4. **
  5. **  This source code is in "shrouded" form. It is distributed in this form
  6. **  rather than as a library (.LIB) file because of the inconsistancies
  7. **  between object files generated by different compilers. To support several
  8. **  compilers would require a .LIB file for each compiler manufacturer, and
  9. **  sometimes several versions of the .LIB file are needed for the different
  10. **  versions of the same manufacturers compiler!
  11. **
  12. **  You can compile this code, but you will have to register with us in order
  13. **  to get the normal (commented) C source code with normal variable names.
  14. */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include "opcodes.h"
  22. #include "si.h"
  23. #include "pcl4c.h"
  24. #include "term.h"
  25. #include "term_io.h"
  26. #include "modem_io.h"
  27. #include "amodem.h"
  28. #include "xymodem.h"
  29. #include "zmodem.h"
  30. #include "win_io.h"
  31. #include "ascii.h"
  32.  
  33. #ifndef FALSE
  34. #define FALSE 0
  35. #define TRUE !FALSE
  36. #endif
  37.  
  38. #define MARK 0x55
  39. #define BYTE unsigned char
  40.  
  41. #define CODE_SIZE 1024
  42. #define DATA_SIZE 1024
  43. #define STACK_SIZE  32
  44.  
  45. #define BUFBLK_SIZE 1024
  46.  
  47. static int  V43 = 2;     
  48. static BYTE V5 = 0;    
  49. static int  V8 = 0;      
  50. static int  V13 = 0;      
  51. static int  V38 = 0;    
  52. static char V25;         
  53. static int  V26 = 1;    
  54. static int  V29 = 540;   
  55. static int  V27 = 5;     
  56. static int  V24 = TRUE;  
  57. static char V28 = 'X';  
  58. static FILE *V3;     
  59. static char V16[15];    
  60. static BYTE V6[CODE_SIZE];   
  61. static char V10[DATA_SIZE];   
  62. static BYTE V37[STACK_SIZE]; 
  63. static int  V2;
  64. static int  V23;
  65. static int  V11;
  66. static int  V39;
  67. static int  V15[10] =
  68.   {0x180,0x0C0,0x060,0x030,0x018,0x00C,0x006,0x003,0x002,0x001};
  69. static char V4[BUFBLK_SIZE];
  70. static int  V40 = FALSE;
  71. static char V41[80];
  72.  
  73. void SaySiErr(int Code)
  74. {
  75.  switch(Code)
  76.    {case  SI_CANNOT_OPEN:
  77.       WinPutString(SCR_WIN,"Cannot open script binary\n");
  78.       break;
  79.     case  SI_UNEXPECTED_EOF:
  80.       WinPutString(SCR_WIN,"Unexpected EOF\n");
  81.       break;
  82.     case  SI_NOT_SCRIPT_BINARY:
  83.       WinPutString(SCR_WIN,"Not script binary\n");
  84.       break;
  85.     case  SI_NOT_CURRENT_VERSION:
  86.       WinPutString(SCR_WIN,"Incorrect script version\n");
  87.       break;
  88.     case  SI_CODE_LENGTH_OVERFLOW:
  89.       WinPutString(SCR_WIN,"Code overflow\n");
  90.       break;
  91.     case  SI_DATA_LENGTH_OVERFLOW:
  92.       WinPutString(SCR_WIN,"Data overflow\n");
  93.       break;
  94.     case  SI_BAD_OPCODE:
  95.       WinPutString(SCR_WIN,"Bad opcode encountered\n");
  96.       break;
  97.     case  SI_USER_ABORTS:
  98.       WinPutString(SCR_WIN,"User aborting...\n");
  99.       break;
  100.     case  SI_STACK_OVERFLOW:
  101.       WinPutString(SCR_WIN,"Stack overflow\n");
  102.       break;
  103.     case  SI_STACK_UNDERFLOW:
  104.       WinPutString(SCR_WIN,"Stack underflow\n");
  105.       break;
  106.     case  SI_BAD_CHECKSUM:
  107.       WinPutString(SCR_WIN,"Bad checksum\n");
  108.       break;
  109.     default:
  110.       sprintf(V41,"Script error %d\n",Code);
  111.       WinPutString(SCR_WIN,V41);
  112.       break;
  113.    }
  114. }
  115.  
  116. static int V18(void)
  117. {int i, k;
  118.  i = fgetc(V3);
  119.  if(i==EOF) return(SI_UNEXPECTED_EOF);
  120.  V5 ^= (BYTE)i;
  121.  return((BYTE)i);
  122. }
  123.  
  124. static int V33(int Item)
  125. {V37[V38++] = Item;
  126.  if(V38==STACK_SIZE) return(SI_STACK_OVERFLOW);
  127.  return (0);
  128. }
  129.  
  130. static int V30(void)
  131. {if(V38==0) return(SI_STACK_UNDERFLOW);
  132.  return (V37[--V38]);
  133. }
  134.  
  135. static char V36(int Flag)
  136. {char c;
  137.  if(Flag) return('T');
  138.  else return('F');
  139. }
  140.  
  141.  
  142. static int V17(int V14)
  143. {int i;
  144.  for(i=0;i<10;i++) if(V14==V15[i]) return i;
  145.  return -1;
  146. }
  147.  
  148.  
  149.  
  150. int si(int Port,char *FilePtr,int Debug)
  151. {int i, k;
  152.  int V35;
  153.  char c;
  154.  char *V32;
  155.  int V21; 
  156.  int V22;   
  157.  int V1;   
  158.  int V7;    
  159.  int V12;    
  160.  int V9;
  161.  int V31;
  162.  int V19;
  163.  char V34 = '"';
  164.  char V42[80];
  165.  int V14;
  166.  char NCGchar = 'C';
  167.  
  168.  V14 = SioGetDiv(Port);
  169.  V2 = V17(V14);
  170.  V31 = SioRead(Port,3);
  171.  V11 = 0x03 & V31;
  172.  V39 = 0x01 & (V31>>2);
  173.  V23 = 0x07 & (V31>>3);
  174. #if 0
  175.  sprintf(V41,"Codes: Baud=%d Data=%d Stop=%d Parity=%d\n",
  176.    V2,V11,V39,V23);
  177.  WinPutString(SCR_WIN,V41);
  178. #endif
  179.  
  180.  if(V2<=Baud19200) V40 = TRUE;
  181.  else V40 = FALSE;
  182.  
  183.  for(i=0;i<CODE_SIZE;i++) V6[i] = OPC_HALT;
  184.  for(i=0;i<DATA_SIZE;i++) V10[i] = 0;
  185.  V5 = 0;
  186.  V8 = 0;
  187.  V13 = 0;
  188.  V38 = 0;
  189.  V25 = TRUE;
  190.  V26 = 1;
  191.  
  192.  strncpy(V16,FilePtr,8);
  193.  strcat(V16,".sb");
  194.  
  195.  V3 = fopen(V16,"rb");
  196.  if(V3==NULL) return (SI_CANNOT_OPEN);
  197.  
  198.  if((V35=V18())<0) return(V35);
  199.  if(V35!=MARK) return(SI_NOT_SCRIPT_BINARY);
  200.  
  201.  if((V35=V18())<0) return(V35);
  202.  if(V35!=V43) return(SI_NOT_CURRENT_VERSION);
  203.  
  204.  if((V35=V18())<0) return(V35);
  205.  V7 = 0x00ff & V35;
  206.  if((V35=V18())<0) return(V35);
  207.  V7 = 256*V7 + (0x00ff&V35);
  208. #if 0
  209. sprintf(V41,"Code Length = %d \n",V7)
  210. WinPutString(SCR_WIN,V41);
  211. #endif
  212.  if(V7>=CODE_SIZE) return(SI_CODE_LENGTH_OVERFLOW);
  213.  
  214.  if((V35=V18())<0) return(V35);
  215.  V12 = 0x00ff & V35;
  216.  if((V35=V18())<0) return(V35);
  217.  V12 = 256*V12 + (0x00ff&V35);
  218. #if 0
  219. sprintf(V41,"Data Length = %d \n",V12);
  220. WinPutString(SCR_WIN,V41);
  221. #endif
  222.  if(V12>=DATA_SIZE) return(SI_DATA_LENGTH_OVERFLOW);
  223.  
  224.  for(i=0;i<V7;i++)
  225.    {if((V35=V18())<0) return(V35);
  226. #if 0
  227. sprintf(Temp"%d: %d\n",i,0x003f&V35);
  228. WinPutString(SCR_WIN,V41);
  229. #endif
  230.     V6[V8++] = (BYTE)V35;
  231.    }
  232.  
  233.  if((V35=V18())<0) return(V35);
  234.  if(V35!=MARK) return(SI_NOT_SCRIPT_BINARY);
  235.  
  236.  for(i=0;i<V12;i++)
  237.    {if((V35=V18())<0) return(V35);
  238. #if 0
  239. sprintf(V41,"%d: %d\n",i,V35);
  240. WinPutString(SCR_WIN,V41);
  241. #endif
  242.     V10[V13++] = (BYTE)V35;
  243.    }
  244.  
  245.  if((V35=V18())<0) return(V35);
  246.  if(V35!=MARK) return(SI_NOT_SCRIPT_BINARY);
  247.  
  248.  V9 = V5;
  249.  if((V35=V18())<0) return(V35);
  250.  
  251.  if((BYTE)V35!=V9)
  252.    {
  253. #if 1
  254.     sprintf(V41,"Received V5=%d\n",(BYTE)V35);
  255.     WinPutString(SCR_WIN,V41);
  256.     sprintf(V41,"Computed V5=%d\n",V9);
  257.     WinPutString(SCR_WIN,V41);
  258. #endif
  259.     return(SI_BAD_CHECKSUM);
  260.    }
  261.  
  262.  
  263.  V8 = 0;
  264.  while(1)
  265.    {if(SioKeyPress())
  266.      {c = SioKeyRead();
  267.       if(c==ESC) return(SI_USER_ABORTS);
  268.       else CharPut(Port,c);
  269.      }
  270.     
  271.     k = V6[V8++];
  272.     V21 = 0x003F & k;
  273.     V22 = (0x00C0 & k) << 2;
  274.     if(Debug)
  275.       {V19 = MatchOpCode(V21);
  276.        sprintf(V41,"@%d",V8);
  277.        sprintf(V42,"[%4s: %s ",V41,GetOpText(V19));
  278.        WinPutString(SCR_WIN,V42);
  279.       }
  280.     
  281.     if(V21>=8)
  282.       {V1 = V22 | (0x00ff & V6[V8++]);
  283.        if(Debug) switch(GetOperType(V19))
  284.          {case CODE_REF:
  285.             sprintf(V41," %d]\n",V1);
  286.             WinPutString(SCR_WIN,V41);
  287.             break;
  288.           case DATA_REF:
  289.             sprintf(V41," %c%s%c]\n",V34,&V10[V1],V34);
  290.             WinPutString(SCR_WIN,V41);
  291.          }
  292.       }
  293.     
  294.     switch(V21)
  295.      {case OPC_HALT:
  296.         return(0);
  297.       case OPC_STATUS:
  298.         sprintf(V41,"PgmChar=%c (%xH) Count=%d Wait=%d Pace=%d Case=%c Protocol=%c\n",
  299.            V36(V25),V25,V26,V29,V27,V36(V24),
  300.            V28);
  301.         WinPutString(SCR_WIN,V41);
  302.         sprintf(V41,"CodePC=%d V21=%d V1=%d V31=%xH BaudDiv=%xH\n",
  303.            V8,V21,V1,SioRead(Port,3),SioGetDiv(Port));
  304.         WinPutString(SCR_WIN,V41);
  305.         ModemDump();
  306.         break;
  307.       case OPC_DELAY:
  308.         SioDelay( (int) (18.2*atof(&V10[V1])) );
  309.         break;
  310.       case OPC_CALL:
  311.         if((V35=V33(V8))<0) return V35;
  312.         V8 = V1;
  313.         break;
  314.       case OPC_RETURN:
  315.         V35 = V30();
  316.         if(V35<0) return V35;
  317.         V8 = V35;
  318.         break;
  319.       case OPC_BAUD:
  320.         i = BaudMatch(&V10[V1]);
  321.         if(V2==-1) V25 = FALSE;
  322.         else
  323.           {V2 = i;
  324.            SioBaud(Port,V2);
  325.            V25 = TRUE;
  326.           }
  327.         break;
  328.       case OPC_DATABITS:
  329.         i = (int) atoi(&V10[V1]);
  330.         V25 = TRUE;
  331.         switch(i)
  332.           {case 7:
  333.              V11 = WordLength7;
  334.              break;
  335.            case 8:
  336.              V11 = WordLength8;
  337.              break;
  338.            default:
  339.              V25 = FALSE;
  340.              break;
  341.           }
  342.         if(V25) SioParms(Port,V23,V39,V11);
  343.         break;
  344.       case OPC_STOPBITS:
  345.         i = (int) atoi(&V10[V1]);
  346.         V25 = TRUE;
  347.         switch(i)
  348.           {case 1:
  349.              V39 = OneStopBit;
  350.              break;
  351.            case 2:
  352.              V39 = TwoStopBits;
  353.              break;
  354.            default:
  355.              V25 = FALSE;
  356.              break;
  357.           }
  358.         if(V25) SioParms(Port,V23,V39,V11);
  359.         break;
  360.       case OPC_PARITY:
  361.         c = V10[V1];
  362.         V25 = TRUE;
  363.         switch(c)
  364.           {case 'N':
  365.              V23 = NoParity;
  366.              break;
  367.            case 'E':
  368.              V23 = EvenParity;
  369.              break;
  370.            case 'O':
  371.              V23 = OddParity;
  372.              break;
  373.            default:
  374.              V25 = FALSE;
  375.              break;
  376.           }
  377.         if(V25) SioParms(Port,V23,V39,V11);
  378.         break;
  379.       case OPC_REPLY:
  380.         ModemSendTo(Port,V27,&V10[V1]);
  381.         break;
  382.       case OPC_SETCOUNT:
  383.         V26 = atoi(&V10[V1]);
  384.         break;
  385.       case OPC_SETWAIT:
  386.         V29 = (int) (18.2*atof(&V10[V1]));
  387.         break;
  388.       case OPC_LOOP:
  389.         if(--V26>0) V8 = V1;
  390.         break;
  391.       case OPC_IFTRUE:
  392.         if(V25) V8 = V1;
  393.         break;
  394.       case OPC_IFFALSE:
  395.         if(!V25) V8 = V1;
  396.         break;
  397.       case OPC_IF:
  398.         if(V25!=V10[V1]) V8 += 2;
  399.         break;
  400.       case OPC_IFNOT:
  401.         if(V25==V10[V1]) V8 += 2;
  402.         break;
  403.       case OPC_GOTO:
  404.         V8 = V1;
  405.         break;
  406.       case OPC_TEST:
  407.         V25 = V10[V1];
  408.         break;
  409.       case OPC_ACCEPT:    
  410.         WinGetString(SCR_WIN,&V10[V1],15);
  411.         break;
  412.       case OPC_SAY:
  413.         V32 = &V10[V1];
  414.         for(i=0;;i++)
  415.           {c = *V32++;
  416.            if(c=='\0') break;
  417.            if(c=='^') c = ( *V32++ ) - '@';
  418.            WinPutChar(SCR_WIN,c);
  419.           }
  420.         break;
  421.       case OPC_WAITFOR:
  422.         V25 = ModemWaitFor(Port,V29,V24,&V10[V1]);
  423.         break;
  424.       case OPC_NOP:
  425.         break;
  426.       case OPC_DEBUG:
  427.         ModemDebug();
  428.         break;
  429.       case OPC_SETPACE:
  430.         V27 = (int) (18.2*atof(&V10[V1]));
  431.         break;
  432.       case OPC_SETCASE:
  433.         c = toupper(V10[V1]);
  434.         if(c=='T') V24 = TRUE;
  435.         if(c=='F') V24 = FALSE;
  436.         break;
  437.       case OPC_QUIET:
  438.         ModemQuiet(Port, (int) (18.2*atof(&V10[V1])) );
  439.         break;
  440.       case OPC_HANGUP:
  441.         ModemHangup(Port);
  442.         break;
  443.       case OPC_PROTOCOL:
  444.         c = toupper(V10[V1]);
  445.         if(c=='A') V28 = 'A';
  446.         if(c=='X') V28 = 'X';
  447.         if(c=='Y') V28 = 'Y';
  448.         if(c=='Z') V28 = 'Z';
  449.         break;
  450.       case OPC_SEND:
  451.         if(V28!='A') ModemEcho(Port,10);
  452.         switch(V28)
  453.           {case 'A':
  454.              
  455.              TxAscii(Port,&V10[V1],V4,BUFBLK_SIZE,FALSE,0,CAN,TRUE);
  456.              break;
  457.            case 'X':
  458.              
  459.              XmodemTx(Port,&V10[V1],V4,FALSE);
  460.              break;
  461.            case 'Y':
  462.              
  463.              YmodemTx(Port,&V10[V1],V4);
  464.              break;
  465.            case 'Z':
  466.              
  467.              ZmodemTx(Port,V16,V40);
  468.              break;
  469.           }
  470.         break;
  471.       case OPC_RECEIVE:
  472.         if(V28!='A') ModemEcho(Port,10);
  473.         switch(V28)
  474.            {case 'A':
  475.                
  476.                RxAscii(Port,&V10[V1],V4,BUFBLK_SIZE,GetBufSize(),FALSE,
  477.                        0x00,5,TRUE);
  478.                break;
  479.             case 'X':
  480.                
  481.                XmodemRx(Port,&V10[V1],V4,NCGchar);
  482.                break;
  483.             case 'Y':
  484.                
  485.                YmodemRx(Port,V4,NCGchar);
  486.             case 'Z':
  487.                
  488.                ZmodemRx(Port,V40);
  489.                break;
  490.            }
  491.         ModemSendTo(Port,V27,"!");
  492.         break;
  493.       case OPC_USER1:
  494.       case OPC_USER2:
  495.       case OPC_USER3:
  496.       case OPC_USER4:
  497.       case OPC_USER5:
  498.       case OPC_USER6:
  499.       case OPC_USER7:
  500.       case OPC_USER8:
  501.         break;
  502.      }
  503.    } 
  504.